
//#include "AI7.h"


/*takes wins as a n by 4 2D array and looks at the different wins and evaluates which player won
 and their point values
 
 This function has to do 3 things:
 1) figure out which player won - return positive or negative values
 2) Figure out what point value they won with - arrangment of the peices
 3) If two players have the same winning value - return 1 point
 */
int evalWin(char wins[150][4])
{
	
    int i = 0;
    int points = 0;
    int bestwin = 0;
    int worstwin = 0;
    int redpoints = 0;
    int bluepoints = 0;
    do{
        ////fprintf(f, f, "checking case %s\n", wins[i]);
        //check for tie - if there is nothing in the wins array
        if(wins[0][0] == '\0' || wins[0][0] == 0)
        {
            ////fprintf(f, "0");//no wins in array - no nothing in array
            points = 0;
            return 0;
        }
        else//we have at least one win - check it out
        {
            //first - check for the good wins - where the first two char's will match
            if(wins[i][0] == wins[i][1])//same colour first two pieces
            {
				
                if(wins[i][0] == 'r')
                {
                    redpoints += 5;
                    points = -5;
                }//we won
                else if(wins[i][0] == 'b')
                {
                    bluepoints += 5;
                    points = 5;
                }
				
                else if(wins[i][0] == 'g')//don't know who won yet
                {
                    if(wins[i][2] == 'r')
                    {
                        redpoints += 5;
                        points = -5;
                    }//we won
                    else
                    {
                        bluepoints += 5;
                        points = 5;
                    }
                }
            }
			
            else if(wins[i][1] == wins[i][2])//middle two are same colour - 4 points
            {
				
                if(wins[i][0] == 'r')
                {
                    redpoints += 4;
                    points = -4;
                }//we won
                else if(wins[i][0] == 'b')
                {
                    bluepoints += 4;
                    points = 4;
                }
                else//first piece is green
                {//check the second piece
                    if(wins[i][1] == 'r')
                    {
                        redpoints += 4;
                        points = -4;
                    }//we won  
                    else if(wins[i][1] == 'b')
                    {
                        bluepoints += 4;
                        points = 4;
                    }
					
                }
				
            }
            else //3 points will be awarded
            {
                if(wins[i][0] == 'r')
                {
                    redpoints += 3;
                    points = -3;
                }//we won
                else if(wins[i][0] == 'b')
                {
                    bluepoints += 3;
                    points = 3;
                }
                else//first piece is green
                {//check the second piece
                    if(wins[i][1] == 'r')
                    {
                        redpoints += 3;
                        points = -3;
                    }//we won
                    else if(wins[i][1] == 'b')
                    {
                        bluepoints += 3;
                        points = 3;
                    }
                }
            }
			
			
			
        }
        if(points > 0)
        {
            if(bestwin < points)
                bestwin = points;
        }
        else
            if(worstwin > points)
                worstwin = points;
		
        i++;
    }while(wins[i][0] != 0);
	
	
    if( redpoints - bluepoints > 0)
        points = worstwin;
    else if(redpoints - bluepoints == 0)
        points = 1;
    else
        points = bestwin;
	
    ////fprintf(f,  "%d\n", points);
    return points;
}

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
int readBoard(char board[columns+2][rows+2])
{
    int i, j, p = 0;
	
    // Populate board array
    for (i = 0; i < columns+2; i++)
    {
        // Padding columns on left and right sides of board
        if (i == 0 || i == columns+1)
        {
            for(j = 0; j < rows+2; j++)
                board[i][j] = SPACE;
            continue;
        }
        // Padding Row over bottom of board
        board[i][0] = SPACE;
        for (j = 1; j < rows+1; j++)
        {
            scanf(",%c", &board[i][j]);
            if(board[i][j] < 's')
				p++;
        }
        // Padding row over top of board
        board[i][j] = SPACE;
    }
    return p;
}

void findWins(char board[columns+2][rows+2], char wins[150][4], int last_played)
{
    // last_piece_x and last_piece_y hold co-ordinates of the last played piece, that is the piece that must be checked for wins
    int last_piece_x = last_played, last_piece_y, winIndex = 0;
    int i;
    // get the last piece's Y co-ordinate
    for (i=1;i<rows+1;i++)
    {
        if(board[last_piece_x][i] == SPACE)
            break;
        last_piece_y = i;
    }
	
    //fprintf(f, "Last Moved Piece's co-ordinates:%i, %i, %c\n", last_piece_x, last_piece_y, board[last_piece_x][last_piece_y]);  
	
    /*
     * How this works is: For each directional axis (X, Y, Diagonal Down Left-to-Right, and Diagonal Up Left-to-Right), the last dropped
     * piece can be in 4 vectors (it can be in positions 0-3). For each of these possible positions, the algorithm grabs the 4 pieces
     * and then checks that vector to see if it is a win. If so, it adds it to the wins array.
     */
	
    // Diagonal Top Left<->Bottom Right
    //fprintf(f, "Beginning Diagonal Loop\n");
    for(i=0;i<4;i++) // Check each of the 4 possible positions for the last dropped piece
    {
        short int offset;
        short int size = 1;
		
        //fprintf(f, "for position:%i\n", i);
		
        // The co-ordinates used to build the vector to check on. Note these move diagonally to the right,down.
        short int start_piece_x = last_piece_x + i;
        short int start_piece_y = last_piece_y - i;
        //fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        // If the starting piece is a space, stop moving in that direction
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            //fprintf(f, "BREAK1: Found Space, finished looking at diagonal!\n", i);
            break;
        }
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x-offset][start_piece_y+offset] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }  
            win[size++] = board[start_piece_x-offset][start_piece_y+offset];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    // Top<->Bottom
    ////fprintf(f, "Beginning Vertical Loop\n");
    for(i=0;i<4;i++)
    {
        short int offset, size = 1;
        ////fprintf(f, "for position:%i\n", i);
        short int start_piece_x = last_piece_x, start_piece_y = last_piece_y - i;
        ////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            ////fprintf(f, "BREAK1: Found Space, finished looking at Vertical!\n", i);
            break;
        }
		
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x][start_piece_y+offset] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }
            win[size++] = board[start_piece_x][start_piece_y+offset];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    // Diagonal Bottom Left<->Top Right
    ////fprintf(f, "Beginning 2nd Diagonal Loop\n");
    for(i=0;i<4;i++)
    {
        short int offset, size = 1;
        ////fprintf(f, "for position:%i\n", i);
		
        short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y - i;
        ////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            ////fprintf(f, "BREAK1: Found Space, finished looking at 2nd diagonal!\n", i);
            break;
        }
		
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x+offset][start_piece_y+offset] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }
			
            win[size++] = board[start_piece_x+offset][start_piece_y+offset];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    //  Left<->Right
    ////fprintf(f, "Beginning Horizontal Loop\n");
    for(i=0;i<4;i++)
    {
        short int offset, size = 1;
        ////fprintf(f, "for position:%i\n", i);
		
        short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y;
        ////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            ////fprintf(f, "BREAK1: Found Space, finished looking at Horizontal!\n", i);
            break;
        }
		
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x+offset][start_piece_y] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }
			
            win[size++] = board[start_piece_x+offset][start_piece_y];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    // End with "end of wins" flag vector, to signal the end of wins
    wins[winIndex][0] = END_OF_WINS;
}

// Print Board function for debugging
void printBoard(char board[columns+2][rows+2])
{
    int i, j;
    // TEST: Print out board
    for (i = 0; i < rows+2; i++)
    {
        //fprintf(f, "\t");
        for (j = 0; j < columns+2; j++)
        {
            //fprintf(f, "%c ", board[j][i]);
        }
        //fprintf(f, "\n");
    }
    //fprintf(f, "\n");
}

int isWin(char board[columns+2][rows+2], int last_played)
{
    //fprintf(f, "opened");
    //scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
    //char board[columns+2][rows+2];
    // How many possible simultaneous wins can there be?
    char wins[150][4];
    //fprintf(f, "For last_played = %i:\n", last_played);
    //readBoard(board);
    //printBoard(board);
    findWins(board, wins, last_played);
    return evalWin(wins);
	
}
